package org.example.rental.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.example.rental.common.Result;
import org.example.rental.domain.Brand;
import org.example.rental.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/brand")
public class BrandController extends BaseController {

    @Autowired
    BrandService brandService;


    @GetMapping("/info/{id}")
    public Result info(@PathVariable(name = "id") Long id){
        Brand brand = brandService.getById(id);

        return Result.succ(brand);
    }


    @GetMapping("/list")
    public Result list(String name){


        Page<Brand> page = brandService.page(getPage(),new QueryWrapper<Brand>().like(StrUtil.isNotBlank(name),"name",name));
        System.out.println(page.getTotal());

        return Result.succ(page);
    }




    @PostMapping("/save")
    public Result save(@Validated @RequestBody Brand brand){

        brandService.save(brand);
        return Result.succ(brand);
    }


    @PostMapping("/update")
    public Result update(@Validated @RequestBody Brand brand){
        brandService.updateById(brand);
        return Result.succ(brand);
    }


    @PostMapping("/delete")
    @Transactional
    public Result delete(@RequestBody Long[] ids){
        //删除品牌
        brandService.removeByIds(Arrays.asList(ids));


        return Result.succ("品牌删除成功");

    }


    //查询所有品牌
    @GetMapping("/home/selectBrands")
    public Result selectHomeBrands(){
        List<Brand> list = brandService.list();
        return Result.succ(list);
    }

    //查询所有品牌
    @GetMapping("/selectBrands")
    public Result selectBrands(){
        List<Brand> list = brandService.list();
        return Result.succ(list);
    }
}
